﻿using System;
using System.Windows.Forms;
using System.Diagnostics;
using NutLog;

namespace Nut.utils
{
    public class ProcessUtil
    {

        private RichTextBox textBox = null;
        /// <summary>
        /// 记录信息的富文本框对象
        /// </summary>
        public RichTextBox RichTextBox
        {
            get
            {
                return textBox;
            }

            set
            {
                textBox = value;
            }
        }

        /// <summary>
        /// 同步获取执行命令的输出
        /// </summary>
        /// <param name="filename"></param>
        /// <param name="arguments"></param>
        /// <returns></returns>
        public string syn_get_process_out(String filename, String arguments)
        {
            LogHelp.trace(GetType(), "filename="+filename + ";arguments="+arguments);
            using (Process process = new System.Diagnostics.Process())
            {
                process.StartInfo.FileName = filename;
                process.StartInfo.Arguments = arguments;
                // 必须禁用操作系统外壳程序  
                process.StartInfo.UseShellExecute = false;
                process.StartInfo.CreateNoWindow = true;
                process.StartInfo.RedirectStandardOutput = true;

                process.Start();

                string output = process.StandardOutput.ReadToEnd();



                process.WaitForExit();
            
                process.Close();
                if (String.IsNullOrEmpty(output) == false)
                {
                    return output;
                }
                return "";

            }
        }
        /// <summary>
        /// 异步获取命令输出
        /// </summary>
        /// <param name="filename"></param>
        /// <param name="arguments"></param>
        public void asyn_get_process_out(String filename, String arguments)
        {
            using (Process process = new System.Diagnostics.Process())
            {
                process.StartInfo.FileName = filename;
                process.StartInfo.Arguments = arguments;
                // 必须禁用操作系统外壳程序  
                process.StartInfo.UseShellExecute = false;
                process.StartInfo.CreateNoWindow = true;
                process.StartInfo.RedirectStandardOutput = true;

                process.Start();

                // 异步获取命令行内容  
                process.BeginOutputReadLine();

                // 为异步获取订阅事件  
                process.OutputDataReceived += new DataReceivedEventHandler(process_OutputDataReceived);
            }
        }

        public void asyn_get_process_out(string dir,String filename, String arguments)
        {
            using (Process process = new System.Diagnostics.Process())
            {
                process.StartInfo.WorkingDirectory=dir;
                process.StartInfo.FileName = filename;
                process.StartInfo.Arguments = arguments;
                // 必须禁用操作系统外壳程序  
                process.StartInfo.UseShellExecute = false;
                process.StartInfo.CreateNoWindow = true;
                process.StartInfo.RedirectStandardOutput = true;

                process.Start();


                // 异步获取命令行内容  
                process.BeginOutputReadLine();

                // 为异步获取订阅事件  
                process.OutputDataReceived += new DataReceivedEventHandler(process_OutputDataReceived);
            }
        }

        private void process_OutputDataReceived(object sender, DataReceivedEventArgs e)
        {
            // 这里仅做输出的示例，实际上您可以根据情况取消获取命令行的内容  
            // 参考：process.CancelOutputRead()  

            if (String.IsNullOrEmpty(e.Data) == false)
                this.AppendText(e.Data + "\r\n");
        }

        #region 解决多线程下控件访问的问题

        public delegate void AppendTextCallback(string text);

        public void AppendText(string text)
        {
            if (textBox.InvokeRequired)
            {
                AppendTextCallback d = new AppendTextCallback(AppendText);
                textBox.Invoke(d, text);
            }
            else
            {
                textBox.AppendText(text);
            }
        }

        #endregion
    }
}
